package com.yopuo.port.controller;

import com.yopuo.common.constant.Constants;
import com.yopuo.common.core.controller.BaseController;
import com.yopuo.common.core.domain.AjaxResult;
import com.yopuo.common.exception.msg.CdPortMsg;
import com.yopuo.common.utils.StringUtils;
import com.yopuo.framework.web.service.SysLoginService;
import com.yopuo.port.facade.SysMenuCdiskFacade;
import com.yopuo.port.util.UserCodeUtil;
import com.yopuo.port.vo.AuthLoginVO;
import com.yopuo.port.vo.CloudDiskReqVo;
import com.yopuo.port.vo.CloudDiskRespVo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

/**
 * 菜单信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/port/cloudDisk")
public class CloudDiskController extends BaseController
{
    @Resource
    private SysLoginService loginService;
    @Resource
    private SysMenuCdiskFacade sysMenuCdiskFacade;

    /**
     * 通过云盘跳转过来免登录出路
     * @return AjaxResult
     */
    @PostMapping("/authLogin")
    public AjaxResult authLogin(@RequestBody AuthLoginVO param){
        String authCode = param.getA();
        Integer userId = param.getUi();
        Long timestamp = param.getT();

        if(StringUtils.isBlank(authCode) || userId == null || timestamp == null){
            return AjaxResult.error("授权登录失败，缺少必要参数，请重新授权登录。");
        }

        if(!UserCodeUtil.checkUserAuthCode(authCode,timestamp,userId)){
            return AjaxResult.error("授权登录超时，请从云盘重新授权登录。");
        }

        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.loginNoPasser(userId.longValue());
        ajax.put(Constants.TOKEN, token);
        return ajax;

    }


    /**
     * 云盘接口路由
     */
    @PostMapping("/route")
    public CloudDiskRespVo routeList(@RequestBody CloudDiskReqVo cloudDiskReqVo)
    {
        CloudDiskRespVo cloudDiskRespVo = new CloudDiskRespVo();
        String dataJson;
        try {
            if (StringUtils.isEmpty(cloudDiskReqVo.getMethodName())) {
                throw new RuntimeException(CdPortMsg.PARAM_NON_EXIST);
            }
            switch (cloudDiskReqVo.getMethodName()){
                //同步菜单（新增、编辑、移动、复制）
                case "syncMenu":
                    sysMenuCdiskFacade.doSyncMenu(cloudDiskReqVo);
                    break;
                //同步用户（新增、编辑）
                case "syncUser":
                    sysMenuCdiskFacade.doSyncUser(cloudDiskReqVo);
                    break;
                //查询权限菜单
                case "queryUserFile":
                    dataJson = sysMenuCdiskFacade.doQueryUserFile(cloudDiskReqVo);
                    cloudDiskRespVo.setData(dataJson);
                    break;
                //删除权限菜单
                case "deleteMenu":
                    sysMenuCdiskFacade.doDeleteMenu(cloudDiskReqVo);
                    break;
                //查询用户是否为管理员
                case "queryUserRole":
                    dataJson = sysMenuCdiskFacade.doQueryUserRole(cloudDiskReqVo);
                    cloudDiskRespVo.setData(dataJson);
                    break;
                default:
                    break;
            }
            cloudDiskRespVo.setStatus(0);
        } catch (Exception e){
            cloudDiskRespVo.setStatus(1);
            cloudDiskRespVo.setMessage(e.getMessage());
        }
        return cloudDiskRespVo;
    }

}